package com.shanzmoo.core.domain.pay;

import cn.hutool.core.util.NumberUtil;
import com.shanzmoo.db.trade.entity.PayOrderEntity;
import lombok.*;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 获取付款二维请求信息
 * @author Gzhao
 * @date 2020/12/6
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class AliPayOrderQrCodeDto implements Serializable {

    private static final long serialVersionUID = 1758L;

    /**
     * 商户订单号。64 个字符以内的大小，可包含字母、数字、下划线。
     */
    private String outTradeNo;

    /**
     * 	订单总金额，单位为元，精确到小数点后两位，取值范围为 [0.01,100000000]，金额不能为 0
     */
    private String totalAmount;

    /**
     * 订单标题，不可使用特殊字符，如 /，=，& 等
     */
    private String subject;

    public static AliPayOrderQrCodeDto create(PayOrderEntity order) {
        return AliPayOrderQrCodeDto.builder()
                .outTradeNo(order.getPayNo())
                .totalAmount(dealWithTotalAmonut(order.getTotalFee()))
                .subject(order.getBody())
                .build();
    }

    /**
     * 将分转换成元，保留2位小数
     * @param totalFee
     * @return
     */
    private static String dealWithTotalAmonut(Integer totalFee) {
        BigDecimal re = NumberUtil.div(new BigDecimal(totalFee), new BigDecimal(100), 2, RoundingMode.HALF_DOWN);
        return re.toString();
    }

}
